﻿using QFramework;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace HKTools
{
    public class Test_UseUdp : MonoBehaviour, IController
    {
        public IArchitecture GetArchitecture()
        {
            return HK_ArchitectureProvider.Architecture;
        }

        [SerializeField] Button btn_Start7878;
        [SerializeField] Button btn_Start8787;
        [SerializeField] Button btn_Close;

        [Header("单播测试")]

        [SerializeField] Button btn_Unicast7878;
        [SerializeField] Button btn_Unicast8787;

        [Header("多播测试")]

        [SerializeField] Button btn_JoinMulticast;  // 加入多播组按钮
        [SerializeField] Button btn_MulticastDefault;  // 发送多播消息（默认）
        [SerializeField] Button btn_Multicast7878;  // 发送多播消息（7878端口）
        [SerializeField] Button btn_Multicast8787;  // 发送多播消息（8787端口）
        [SerializeField] Button btn_LeaveMulticast; // 离开多播组按钮

        [Header("广播测试")]

        [SerializeField] Button btn_Broadcast8787;
        [SerializeField] Button btn_BroadcastACK8787_01;
        [SerializeField] Button btn_BroadcastACK8787_02;

        // 单播测试地址
        string unicastAddress = "192.168.0.14";
        // 多播组地址（示例使用 D 类地址 224.0.0.100）
        string multicastAddress = "224.0.0.100";

        void Awake()
        {
            DontDestroyOnLoad(gameObject);
        }

        void Start()
        {
            btn_Start7878.onClick.AddListener(() =>
            {
                this.SendCommand(new CMD_UDP_Start(7878));
            });

            btn_Start8787.onClick.AddListener(() =>
            {
                this.SendCommand(new CMD_UDP_Start(8787));
            });

            btn_Close.onClick.AddListener(() =>
            {
                this.SendCommand(new CMD_UDP_Close());
            });

            btn_Unicast7878.onClick.AddListener(() =>
            {
                this.SendCommand(new CMD_UDP_UnicastMsg("单播测试消息", unicastAddress, 7878));
            });

            btn_Unicast8787.onClick.AddListener(() =>
            {
                this.SendCommand(new CMD_UDP_UnicastMsg("单播测试消息", unicastAddress, 8787));
            });

            btn_JoinMulticast.onClick.AddListener(() =>
            {
                // 加入多播组
                this.SendCommand(new CMD_UDP_JoinMulticastGroup(multicastAddress));
            });

            btn_MulticastDefault.onClick.AddListener(() =>
            {
                // 发送多播消息
                this.SendCommand(new CMD_UDP_MulticastMsg("多播测试消息", multicastAddress));
            });

            btn_Multicast7878.onClick.AddListener(() =>
            {
                // 发送多播消息（7878端口）
                this.SendCommand(new CMD_UDP_MulticastMsg("多播测试消息", multicastAddress, 7878));
            });

            btn_Multicast8787.onClick.AddListener(() =>
            {
                // 发送多播消息（8787端口）
                this.SendCommand(new CMD_UDP_MulticastMsg("多播测试消息", multicastAddress, 8787));
            });

            btn_LeaveMulticast.onClick.AddListener(() =>
            {
                // 离开多播组
                this.SendCommand(new CMD_UDP_LeaveMulticast(multicastAddress));
            });

            btn_Broadcast8787.onClick.AddListener(() =>
            {
                this.SendCommand(new CMD_UDP_BroadcastMsg("广播测试消息", 8787));
            });

            btn_BroadcastACK8787_01.onClick.AddListener(() =>
            {
                this.SendCommand(new CMD_UDP_BroadcastAckMsg("广播测试ACK消息01", 8787));
            });

            btn_BroadcastACK8787_02.onClick.AddListener(() =>
            {
                this.SendCommand(new CMD_UDP_BroadcastAckMsg("广播测试ACK消息02", 8787, () => HKDebug.Log("确认发送成功！")));
            });

            this.RegisterEvent<Event_UDP_ReceiveMsg>(args =>
            {
                if (args._msg == "广播测试消息")
                {
                    GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                    sphere.name = args._msg;
                }
                // HKDebug.Log("接收 UDP 消息：" + args._msg);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            this.RegisterEvent<Event_UDP_ReceiveAckMsg>(args =>
            {
                if (args._ackMsg == "广播测试ACK消息01")
                {
                    GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    cube.name = args._ackMsg;
                }
                // HKDebug.Log("接收 UDP Ack消息：" + args._ackMsg);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            this.SendCommand(new CMD_UDP_InitAckList(new List<string>()
            {
                "广播测试ACK消息01","广播测试ACK消息02"
            }));
        }

        void OnDestroy()
        {
            this.SendCommand(new CMD_UDP_Close());
        }

        void OnApplicationQuit()
        {
            this.SendCommand(new CMD_UDP_Close());
        }
    }
}